package com.base.cn.platform.os.controller.mobile.user.center.academic;

import com.base.cn.platform.os.common.MobileBaseController;
import com.base.cn.platform.os.common.utils.DataUtil;
import com.base.cn.platform.os.service.manage.subject.SubjectService;
import com.base.cn.platform.os.service.manage.user.user.CusUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学业Controller
 *
 * @author s.li
 * @create 2018-06-07-17:05
 */
@Controller
@RequestMapping("/mobile/uc/academic")
public class MobileAcademicController extends MobileBaseController {
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CusUserService cusUserService;

    /**
     * 我的学业
     * @param request
     * @return
     */
    @RequestMapping("/myAcademic")
    public String myAcademic(HttpServletRequest request, Model model){
        BigDecimal userId= webMobileLoginUtil.getLoginUserId(request);
        //查询用户学业信息
        Map<String,List<Map<String,Object>>> userAcademicMap = cusUserService.findUserAcademic(userId);
        model.addAttribute("userAcademicMap",userAcademicMap);
        return resultPage("/user/center/academic/u-myAcademic");
    }

    /**
     * 获取用户可以添加的专业列表
     * @param parentIds 排除的专业ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/getSubjectParent")
    public String getSubjectParent(Model model,
                                   @RequestParam(value = "parentIds",required = false) String parentIds){
        Map<String,Object> params = new HashMap<>();
        params.put("parentId",0);
        params.put("notIds",parentIds);
        params.put("status",1);
        List<Map<String,Object>> parentSubjectList = subjectService.findSubjectList(params);
        model.addAttribute("parentSubjectList",parentSubjectList);
        return resultPage("/user/center/academic/parent-subject");
    }

    /**
     * 专业下所有科目
     * @param parentId
     * @return
     */
    @RequestMapping("/querySubjectByParentIds")
    public String querySubjectByParentIds(Model model,
                                          @RequestParam("parentId") BigDecimal parentId,
                                          @RequestParam(value = "childIds",required = false) String childIds){
        if(DataUtil.idIsNotNull(parentId)){
            Map<String,Object> params = new HashMap<>();
            params.put("notIds",childIds);
            params.put("parentId",parentId);
            params.put("status",1);
            List<Map<String,Object>> childSubjectList = subjectService.findSubjectList(params);
            model.addAttribute("childSubjectList",childSubjectList);
            model.addAttribute("parentId",parentId);
        }
        return resultPage("/user/center/academic/child-subject");
    }

    /**
     * 添加专业和科目
     * @return
     */
    @RequestMapping("/save/userAddSubject")
    @ResponseBody
    public Map<String,Object> saveUserAddSubject(HttpServletRequest request,
                                                 @RequestParam("subjectIds")String subjectIds){
        BigDecimal userId = webMobileLoginUtil.getLoginUserId(request);
        return cusUserService.updateUserNaturalSubjectIds(userId, subjectIds);
    }
}
